// pages/register1/index.js
import {
  request
} from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj: [],

    select: false,
    adress: '请选择收货地址',

    region: ['河南省', '郑州市', '中原区'],
    current_css: false,

    Code_input: {},

    // phone: "13083780851",
    timer: "",            // 定时器名字
    countDownNum: '60', // 倒计时初始值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // picker选择地区按钮
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //电话输入框 限制只能输入数字 1公司负责人
  handleInput1(e) {
    let value1 = this.validateNumber1(e.detail.value)


    this.setData({
      value1
    })
  },
  validateNumber1(val) {
    return val.replace(/\D/g, '')
  },
  //电话输入框 限制只能输入数字————结束

  //电话输入框 限制只能输入数字 2采购负责人
  handleInput2(e) {
    let value2 = this.validateNumber2(e.detail.value)
    this.setData({
      value2
    })
  },
  validateNumber2(val) {
    return val.replace(/\D/g, '')
  },
  //电话输入框 限制只能输入数字————结束

  //电话输入框 限制只能输入数字 3收购人
  handleInput3(e) {
    let value3 = this.validateNumber3(e.detail.value)

   

    this.setData({
      value3
    })
  },
  validateNumber3(val) {
    return val.replace(/\D/g, '')
  },
  //电话输入框 限制只能输入数字————结束
  //电话输入框 限制只能输入数字 4绑定手机号
  handleInput4(e) {
    let value4 = this.validateNumber4(e.detail.value)
    // 判断输入11位数字时改变颜色事件   少于11就变暗
    if (value4.toString().length > 10) {
      console.log("数量达到11，发送字体变色", e.detail.value)
      this.setData({
        current_css: true
      })
    } else {
      this.setData({
        current_css: false
      })
    }
    this.setData({
      value4
    })
  },
  validateNumber4(val) {
    return val.replace(/\D/g, '')
  },
  //电话输入框 限制只能输入数字————结束

  // 点击展示下拉框
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },

  //1、点击收货地址
  handleChooseAdress() {
    console.log("触发");
    // 1、获取权限状态      
    wx.getSetting({
      success: (result) => {
        console.log(result)
        // 获取后赋值 当有怪异的属性名时用[""]包起来
        const scopeAddress = result.authSetting["scope.address"];
        if (scopeAddress === true || scopeAddress === undefined) {
          // 2、调用收货地址
          wx.chooseAddress({
            success: (result1) => {
              console.log("拿到了收货地址", result1)

              // 储存到缓存中
              wx.setStorageSync("address", result1);
              // 获取缓存中的购物车数据
              const address = wx.getStorageSync("address") || [];

              // 2、给data数据赋值
              this.setData({
                address
              })
            },
            // fail: () => {},
            // complete: () => {}
          });
        } else {
          // 3.用户如果拒绝权限 诱导他打开授权页面
          wx.openSetting({
            success: (result2) => {
              // 4、可以调用收货地址代码
              wx.chooseAddress({
                success: (result3) => {
                  console.log(result3)
                }
              });
            },
            // fail: () => {},
            // complete: () => {}
          });
        }
      },
      // fail: () => {},
      // complete: () => {}
    });

  },
  // 点击发送验证码
  click_code() {

    // 倒计时功能开始
    var _this = this;
    var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
    var timer = setInterval(function () {
      countDownNum -= 1;
      _this.setData({
        countDownNum: countDownNum
      })
      if (countDownNum <= -1) {
        clearInterval(timer);
        // 取消置顶的setInterval函数将要执行的代码
        _this.setData({
          countDownNum: 60,
        })
      }
    }, 1000)
    // 倒计时功能结束

    request({
        url: 'https://yunbao.greatforest.com.cn/ruoyi-admin/qt/send?phone=' + this.data.value4,
        method: "GET"
      })
      .then(res => {
        // 主页请求的商品数据
        console.log('传回来的验证码数据', res);
        let return_code = res.data.data
        this.setData({
          return_code
        })
      })
  },
  // 判断输入验证码的
  passwordInput(e) {
    console.log("获取输入的验证码", e.detail.value)
    let Code_input = e.detail.value
    this.setData({
      Code_input
    })
  },
  // 确认对比验证码按钮
  zhuche(e) {
    console.log("点击注册")
    if (this.data.Code_input === this.data.return_code) {

    } else {
      wx.showToast({
        title: '验证码错误',
        duration: 1500,
        icon: 'error',
        // image: 'none',
        mask: true,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }
  },

  // 注册按钮

  // 注册提交按钮
  formsubmit(e) {
    console.log(e)
    console.log(e.detail.value)
    var obj = {};
    // 把注册输入的信息放到  定义的空对象中
    // 店铺名
    obj.dpname = e.detail.value.dpname
    // 绑定的手机号
    obj.bdphone = e.detail.value.bdphone
    // 公司负责人
    obj.gsfzrname = e.detail.value.gsfzrname
    obj.gsfzrphone = e.detail.value.gsfzrphone
    // 采购负责人
    obj.cgfzrname = e.detail.value.cgfzrname
    obj.cgfzrphone = e.detail.value.cgfzrphone
    // 收货人
    obj.shrname = e.detail.value.shrname
    obj.phone = e.detail.value.phone

    // 密码
    obj.password = e.detail.value.password
    obj.qrpassword = e.detail.value.qrpassword

    // 收货地址
    obj.saddress = this.data.region[0]
    obj.address = this.data.region[1]
    obj.xaddress = this.data.region[2]

    // 详细地址
    obj.xxaddress = e.detail.value.xxaddress

    console.log("obj账号注册信息", obj)
    this.setData({
      obj
    });

    // 发送异步请求 把数据发送后端审核  请求用promise封装
    wx.request({
      url: 'https://yunbao.greatforest.com.cn/ruoyi-admin/qt/zhuce',
      dataType: 'json',
      responseType: 'text',
      data: [{
        "dpname": this.data.obj.dpname,
        "gsfzrname": this.data.obj.gsfzrname,
        "gsfzrphone": this.data.obj.gsfzrphone,
        "cgfzrname": this.data.obj.cgfzrname,
        "cgfzrphone": this.data.obj.cgfzrphone,
        "shrname": this.data.obj.shrname,
        "phone": this.data.obj.phone,
        "password": this.data.obj.password,
        "qrpassword": this.data.obj.qrpassword,
        "saddress": this.data.obj.saddress,
        "address": this.data.obj.address,
        "xaddress": this.data.obj.xaddress,
        "xaddress": this.data.obj.xaddress,
        "xxaddress": this.data.obj.xxaddress,
        "bdphone": this.data.obj.bdphone,
      }],
      method: "POST",
      success: (e) => {
        //成功回调
        console.log("注册后返回的信息", e)
        // 获取状态码判断
        let zcstate = e.data.data.zcstate
        console.log("注册后返回的值，用来接收判断的", zcstate)

        if (this.data.Code_input === this.data.return_code) {
          if (zcstate === 1) {
            wx.showToast({
              title: e.data.msg,
              duration: 1500,
              success: function () {
                // 跳转前的判断
                setTimeout(() => {
                  wx.switchTab({
                    url: '/pages/user/index',
                  })
                }, 1500);
              }
            })

          }
          if (zcstate === 5) {
            wx.showToast({
              title: e.data.msg,
              icon: 'none',
              duration: 3500,
              success: function () {
                // 不跳转了
              }
            })
          }
          if (zcstate === 4) {
            wx.showToast({
              title: e.data.msg,
              icon: 'none',
              duration: 3500,
              success: function () {
                // 不跳转了
              }
            })
          }
        } else {
          wx.showToast({
            title: '验证码错误',
            duration: 1500,
            icon: 'error',
            // image: 'none',
            mask: true,
            success: (res) => {},
            fail: (res) => {},
            complete: (res) => {},
          })
        }



      }
    })

    // 本地存入用户多个账户
    // 先获取 userobjs 中的数据  之前没有注册数据 就先赋值个空[]
    var arr = wx.getStorageSync('userobjs') || [];
    // 没有就把刚输入的obj加入到arr
    arr.push(obj);
    // 再把arr写入本地
    wx.setStorageSync('userobjs', arr)
    // 存入本地后显示登陆成功
    wx.showToast({
      title: this.data.msg,
      duration: 4000,
      success: function () {
        // wx.navigateTo({
        //   url: '/pages/login/index',
        // })
      }
    })
  },
})